package com.huaxia.day18;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JfreamTest {
    public static void main(String[] args) {
//        createFrame();
//        createFream();
        testLayout();
    }

    public static void createFrame() {
        JFrame frame = new JFrame("学生信息");
        frame.setSize(400, 200);
        frame.setVisible(true);
        JButton submit = new JButton("递交");
        JButton cancel = new JButton("取消");
        frame.add(submit);
        frame.add(cancel);
    }

    /**
     * 流式布局
     */
    public static void createFream() {
        JFrame frame = new JFrame("学生信息");
        frame.setSize(400, 200);
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        JButton submit = new JButton("递交");
        JButton cancel = new JButton("取消");
        JButton close = new JButton("关闭");

        frame.add(submit);
        frame.add(cancel);
        frame.add(close);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     *
     */
    public static void testLayout() {
        JFrame frame = new JFrame("对齐方式");
        frame.setSize(400, 200);
        frame.setVisible(true);

        JButton left = new JButton("左对齐");
        JButton center = new JButton("居中");
        JButton right = new JButton("右对齐");

        JPanel panel = new JPanel();
        FlowLayout fl = (FlowLayout) panel.getLayout();

        panel.add(left);
        panel.add(center);
        panel.add(right);

        frame.setContentPane(panel);
        left.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fl.setAlignment(FlowLayout.LEFT);
                fl.layoutContainer(frame);
            }
        });
        center.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fl.setAlignment(FlowLayout.CENTER);
                fl.layoutContainer(frame);
            }
        });

        //自己写右对齐

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
